package com.tavenli.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.tavenli.entity.F18Device;
import com.tavenli.entity.PersonEntity;

@Repository
public class PersonDao extends BaseDao{
	private static Logger logger = LoggerFactory.getLogger(UserDao.class);

	public PersonEntity getPersonEntityById(int id) {
		try {
			return this.getById(PersonEntity.class, id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	public int queryDataCount(Map<String, Object> paramMap) {
		StringBuilder hql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		hql.append("select count(*) from PersonEntity where 1=1");

		if (paramMap.get("id") != null) {
			hql.append(" and id=:id");
			params.put("id", paramMap.get("id"));
		}

		if (paramMap.get("personStatus") != null) {
			hql.append(" and personStatus=:personStatus");
			params.put("personStatus", paramMap.get("personStatus"));
		}

		return super.queryPageTotalCount(hql.toString(), params).intValue();
	}

	public List<PersonEntity> queryPageData(int start, int maxSize,
			Map<String, Object> paramMap) {
		StringBuilder hql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		hql.append("from PersonEntity  where 1=1");

		if (paramMap.get("id") != null) {
			hql.append(" and id=:id");
			params.put("id", paramMap.get("id"));
		}
		if (paramMap.get("personStatus") != null) {
			hql.append(" and personStatus=:personStatus");
			params.put("personStatus", paramMap.get("personStatus"));
		}

		hql.append(" order by id desc");

		return super.queryPageList(hql.toString(), params, start, maxSize);
	}
	public boolean existPerson(String pin) {
		String hql = "select count(*) from PersonEntity where personPin=?";
		Query query = this.createQuery(hql);
		query.setParameter(1, pin);
		Object obj = query.getResultList().get(0);
		Long count = Long.parseLong(obj.toString());
		return count.compareTo(0L)>0;
	}
	public int delPerson(int Id) {

		String hql = "delete PersonEntity t where t.id=?";

		Query query = this.getEntityManager().createQuery(hql);
		query.setParameter(1, Id);
		try {
			return query.executeUpdate();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return -1;
	}

	public int delPersons(Integer[] Ids) {

		String hql = "delete PersonEntity t where t.id in ("
				+ StringUtils.join(Ids, ",") + ")";

		Query query = this.getEntityManager().createQuery(hql);

		try {
			return query.executeUpdate();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return -1;
	}

	public PersonEntity getPersonEntityByPin(String pinString) {
		try {
			String hql = "from PersonEntity where personPin=?";
			TypedQuery<PersonEntity> query = this.getEntityManager().createQuery(hql, PersonEntity.class);
			query.setParameter(1, pinString);
			if(query.getResultList().size()==0 ){
	            return null;
	        }else{
	        	return query.getSingleResult();
	        }
		} catch (Exception e) {
			logger.error("根据pin号查找用户失败！用户Pin号为："+pinString);
			return null;
		}
		
	}
}
